﻿using System;
using System.Collections.Generic;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using XamarinApp.Droid.Models;
using XamarinApp.Droid.Services;

namespace XamarinApp.Droid
{
	[Activity(Label = "Xamarin Demo App", MainLauncher = true, Icon = "@drawable/icon")]
	public class MainActivity : Activity
	{
		private List<ListItemModel> _items;

		protected override async void OnCreate(Bundle bundle)
		{
			base.OnCreate(bundle);
			SetContentView(Resource.Layout.Main);

			WebService webService = new WebService();
			_items = await webService.FetchLatestPosts();

			ListView listView = FindViewById<ListView>(Resource.Id.listView);
			listView.Adapter = new MyListAdapter(this, _items);

			listView.ItemClick += ListViewOnItemClick;
		}

		private void ListViewOnItemClick(object sender, AdapterView.ItemClickEventArgs e)
		{
			var item = _items[e.Position];

			var intent = new Intent(this, typeof(PostActivity));
			intent.PutExtra("Title", item.Title);
			intent.PutExtra("Content", item.Content);

			StartActivity(intent);
		}
	}
}

